package com.sinya.shoppingmall;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.sinya.shoppingmall.widget.address.AssetsUtils;

import java.util.ArrayList;

import cn.qqtheme.framework.picker.AddressPicker;

public class MyAddressAddedActivity extends AppCompatActivity implements View.OnClickListener {

    private static final int RESULT_CODE = 0;
    private SharedPreferences sp;
    private Button btn_address_save;
    private EditText et_consignee_name, et_consignee_address, et_address_detail;
    private String mProvince, mCity, mCounty;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_address_added);
        sp = getSharedPreferences("location_list", MODE_PRIVATE);

        initViews();

    }

    private void initViews() {

        TextView title = (TextView) findViewById(R.id.tv_title);
        title.setText("添加地址");

        ImageView back = (ImageView) findViewById(R.id.img_back);
        back.setImageResource(R.drawable.btn_title_back_selector);
        back.setOnClickListener(this);
        back.setVisibility(View.VISIBLE);

        et_consignee_name = (EditText) findViewById(R.id.et_consignee_name);
        et_address_detail = (EditText) findViewById(R.id.et_address_detail);
        btn_address_save = (Button) findViewById(R.id.btn_address_save);
        btn_address_save.setOnClickListener(this);
        et_consignee_address = (EditText) findViewById(R.id.et_consignee_address);
        et_consignee_address.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {

            case R.id.img_back:
                finish();
                break;

            case R.id.et_consignee_address:

                ArrayList<AddressPicker.Province> data = new ArrayList<AddressPicker.Province>();
                String json = AssetsUtils.readText(MyAddressAddedActivity.this, "city.json");
                data.addAll(JSON.parseArray(json, AddressPicker.Province.class));
                final AddressPicker picker = new AddressPicker(MyAddressAddedActivity.this, data);

                //默认地址
                mProvince = sp.getString("province", "广东");
                mCity = sp.getString("city", "深圳");
                mCounty = sp.getString("county", "南山区");
                picker.setSelectedItem(mProvince, mCity, mCounty);

                picker.setOnAddressPickListener(new AddressPicker.OnAddressPickListener() {
                    @Override
                    public void onAddressPicked(String province, String city, String county) {

                        et_consignee_address.setText(province + city + county);
                        sp.edit().putString("province", province).commit();
                        sp.edit().putString("city", city).commit();
                        sp.edit().putString("county", county).commit();
                    }
                });
                picker.show();
                break;

            case R.id.btn_address_save:

                if (TextUtils.isEmpty(et_consignee_name.getText().toString()) || TextUtils.isEmpty(et_consignee_address.getText().toString()) || TextUtils.isEmpty(et_address_detail.getText()
                        .toString())) {
                    Toast.makeText(MyAddressAddedActivity.this, "亲，请填写完整的信息哦", Toast.LENGTH_SHORT).show();
                    return;
                }

                String name = et_consignee_name.getText().toString();
                String addressDetail = et_address_detail.getText().toString();
                Intent mIntent = new Intent();
                mIntent.putExtra("name", name);
                mIntent.putExtra("addressDetail", addressDetail);
                mIntent.putExtra("province", mProvince);
                mIntent.putExtra("city", mCity);
                mIntent.putExtra("county", mCounty);
                MyAddressAddedActivity.this.setResult(RESULT_CODE, mIntent);

                finish();
                break;

            default:
                break;
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();

        SharedPreferences.Editor editor = sp.edit();
        editor.clear();
        editor.commit();
    }
}